//角色编辑
var groupid;
var iconid;
var subid;
var menuid;
var id;
$(function() {
  id = ly.getUrlParam("id");
  groupid = ly.getUrlParam("groupid");
  iconid = ly.getUrlParam("iconid");
  subid = ly.getUrlParam("subid");
  menuid = ly.getUrlParam("menuid");
  var t = 0;
  $(".form").getForm({
    'id': id
  }, function(data) {
    if (data.level == 0) {
      $("#menuNameNext").html("顶层菜单");
    }
    $("#isUsing").getSelect({
      type: "radio",
      asName: "isUsing",
      list: [
        { value: "0", display: "禁用" },
        { value: "1", display: "启用" }
      ],
      default: data.isUsing
    });
    // $("#clientType").getSelect({
    //   type: "select",
    //   asName: "clientType",
    //   code: "roleAffiliate",
    //   list: [{
    //     "value": '',
    //     "display": '请选择'
    //   }],
    //   default: data.clientType
    // });

    $("#isUsing").getSelect({
      type: "radio",
      asName: "isUsing",
      list: [
        { value: "0", display: "禁用" },
        { value: "1", display: "启用" }
      ],
      default: data.isUsing
    });
  });

  //保存
  $("body").on("click", "#save_btn_menu", function() {
    save();
  });
});

function save() {
  var obj = packageObj();
  var url = site.apiUrl + "/back/menu/updateMenuThis";
  //ajaxForm(url,obj,submitCallback_update,'POST',false,false);
  request({
    url: url,
    data: obj,
    type: "post",
    ajaxform: 'true',
    callback: function(f) {
      console.log(f);
      if (f.code == 200) {
        submitCallback_update(f);
      } else {
        ly.toast(f.message);
      }
    }
  });

}


function submitCallback_update(data) {
  console.log(data);
  if (data.code == 200) {
    $("#save_btn_menu").attr("disabled", "disabled");
    ly.alert('修改成功', function() {
      ly.goto("menu.html");
    });

  } else {
    ly.toast(data.message);
  }
}
//数据打包
function packageObj() {
  var isUsing = $("#isUsing input:checked").val(); //当前状态

  var menuName = $("#menuName").val(); //菜单标题
  var url = $("#url").val(); //菜单URL
  var nodeOrder = $("#nodeOrder").val(); //顺序
  var icon = $("#icon").val(); //图标
  var createUserId = window.sessionStorage.getItem("backstage-access-id");
  var background = $("#background").val();
  if (id == null && id == '') {
    id = "0";
  }
  var obj = {
    'isUsing': isUsing,
    'menuName': menuName,
    'url': url,
    'nodeOrder': nodeOrder,
    'background': background,
    'updateUserId': createUserId,
    'id': id,
    'icon': icon
  };
  console.log(obj);
  return obj;
}